import 'package:flutter/material.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yp/helper.dart';
import 'package:get/get.dart';
import 'package:yp/services/api.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'logic.dart';
import 'state.dart';

class MyRewardPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _MyRewardPage();
  }
}

class _MyRewardPage extends State<MyRewardPage> {
  final MyRewardLogic logic = Get.put(MyRewardLogic());
  final MyRewardState state = Get.find<MyRewardLogic>().state;

  List member_award_list;
  var num = 0.0;

  getreasrd() {
    var params = {
      "data_sign": 0,
      "channel_num_id": 98,
      "use_platform": 3,
      "access_token": SpUtil.getString('token'),
      "usr_num_id": SpUtil.getInt('empeNumId'),
      "query_sign": 1,
      "page": 1,
      "page_size": 999
    };
    Api.awardquery(params).listen((event) {
      if (event['code'] == 0) {
        num = event['award_integral'];
        var numInt = 0.0;
        if (event['member_award_list'] != null) {
          setState(() {
            member_award_list = event['member_award_list'];
            member_award_list.forEach((element) {
              numInt = numInt + double.parse(element['award_integral'].toString());
              element['isviseble'] = false;
            });
          });
        }
        print('====================== num = $num, *** numInt = $numInt');
      }
    });
  }

  @override
  void initState() {
    // print(Get.arguments);
    getreasrd();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        elevation: 0,
        backgroundColor: Color(0xff0F0B0A),
        brightness: Brightness.dark,
        title: Text(
          '我的奖励',
          style: TextStyle(
            color: Colors.white,
            fontSize: 17,
          ),
        ),
        iconTheme: IconThemeData(
          color: Colors.white,
        ),
      ),
      body: GetBuilder<MyRewardLogic>(
        builder: (logic) {
          return Container(
            color: Color(0xffF5F5F5),
            child: ListView.separated(
              itemBuilder: (context, index) {
                if (index == 0) {
                  return Container(
                    height: setWidth(290),
                    child: Stack(
                      children: [
                        Container(
                          height: setWidth(200),
                          decoration: BoxDecoration(
                            gradient: LinearGradient(
                              begin: Alignment.topCenter,
                              end: Alignment.bottomCenter,
                              colors: [
                                Color(0xffD7AF91),
                                Color(0xffF6E0CC),
                              ],
                            ),
                          ),
                        ),
                        Positioned(
                          top: setWidth(70),
                          left: setWidth(50),
                          child: Container(
                            width: setWidth(650),
                            height: setWidth(220),
                            decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.circular(setWidth(20)),
                              boxShadow: [
                                BoxShadow(
                                  color: Color(0x33333333),
                                  offset: Offset(0, 0), //阴影y轴偏移量
                                  blurRadius: 10,
                                  spreadRadius: .3,
                                ),
                              ],
                            ),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Padding(
                                  padding: EdgeInsets.only(
                                    left: setWidth(30),
                                    top: setWidth(44),
                                    right: setWidth(30),
                                    bottom: setWidth(18),
                                  ),
                                  child: Row(
                                    mainAxisAlignment: MainAxisAlignment.start,
                                    crossAxisAlignment: CrossAxisAlignment.center,
                                    children: [
                                      Container(
                                        width: 80.w,
                                        height: 80.w,
                                        child: ClipRRect(
                                            borderRadius: BorderRadius.circular(setWidth(40)),
                                            child: FadeInImage(
                                                image: NetworkImage(Get.arguments['logo']),
                                                imageErrorBuilder: (context, error, stackTrace) {
                                                  // TODO 图片加载错误后展示的 widget
                                                  // print("---图片加载错误---");
                                                  // 此处不能 setState
                                                  return Image.asset("assets/images/avatar.png");
                                                },
                                                placeholder: AssetImage('assets/images/avatar.png'),
                                                fit: BoxFit.cover)),
                                      ),
                                      SizedBox(
                                        width: setWidth(20),
                                      ),
                                      Text(Get.arguments['usr_name'].toString()),
                                      SizedBox(
                                        width: setWidth(20),
                                      ),
                                    ],
                                  ),
                                ),
                                Padding(
                                  padding: EdgeInsets.symmetric(horizontal: setWidth(20)),
                                  child: Text(
                                    '累计获得奖励：$num积分',
                                    maxLines: 1,
                                    overflow: TextOverflow.ellipsis,
                                    style: TextStyle(
                                      color: Color(0xff999999),
                                      fontSize: 12,
                                    ),
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ),
                      ],
                    ),
                  );
                } else if (index == 1 && member_award_list != null) {
                  return Container(
                    padding: EdgeInsets.only(
                      top: setWidth(40),
                    ),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Padding(
                          padding: EdgeInsets.only(
                            left: setWidth(20),
                            bottom: setWidth(10),
                          ),
                          child: Text(
                            '奖励明细',
                            style: TextStyle(
                              color: Color(0xff210F0F),
                              fontSize: 18,
                            ),
                          ),
                        ),
                        // Container(
                        //   color: Colors.white,
                        //   height: setWidth(80),
                        //   padding: EdgeInsets.symmetric(horizontal: setWidth(20)),
                        //   alignment: Alignment.centerLeft,
                        //   child: Text(
                        //     '2019年12月',
                        //     style: TextStyle(
                        //       color: Color(0xff3A3A3A),
                        //       fontSize: 14,
                        //     ),
                        //   ),
                        // ),
                        SizedBox(
                          height: setWidth(2),
                        ),
                        Column(
                          children: this.member_award_list.map((e) {
                            return GestureDetector(
                              onTap: () {
                                setState(() {
                                  e['isviseble'] = !e['isviseble'];
                                });
                              },
                              child: Container(
                                // height: setWidth(200),
                                color: Colors.white,
                                child: Column(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Divider(
                                      height: setWidth(1),
                                      indent: setWidth(20),
                                      endIndent: 0,
                                      color: Color(0xffEFEFEF),
                                    ),
                                    Container(
                                      padding: EdgeInsets.symmetric(horizontal: setWidth(20)),
                                      child: Row(
                                        children: [
                                          Expanded(
                                            child: Container(
                                              height: setWidth(140),
                                              child: Column(
                                                crossAxisAlignment: CrossAxisAlignment.start,
                                                mainAxisAlignment: MainAxisAlignment.center,
                                                children: [
                                                  Text(
                                                    e['award_type_name'],
                                                    maxLines: 1,
                                                    overflow: TextOverflow.ellipsis,
                                                    style: TextStyle(
                                                      color: Color(0xff4c4c4c),
                                                      fontSize: 14,
                                                    ),
                                                  ),
                                                  SizedBox(
                                                    height: setWidth(20),
                                                  ),
                                                  Text(
                                                    e['award_dtme'],
                                                    maxLines: 1,
                                                    overflow: TextOverflow.ellipsis,
                                                    style: TextStyle(
                                                      color: Color(0xffcccccc),
                                                      fontSize: 11,
                                                    ),
                                                  ),
                                                ],
                                              ),
                                            ),
                                          ),
                                          SizedBox(
                                            width: setWidth(20),
                                          ),
                                          Row(
                                            children: [
                                              Text(
                                                '奖励详情',
                                                style: TextStyle(
                                                  color: Color(0xff999999),
                                                  fontSize: 12,
                                                ),
                                              ),
                                              SizedBox(
                                                width: setWidth(10),
                                              ),
                                              Icon(Icons.arrow_forward_ios,size: setWidth(16),color: Color(0xff999999),)
                                            ],
                                          ),
                                        ],
                                      ),
                                    ),
                                    Divider(
                                      height: setWidth(1),
                                      indent: setWidth(20),
                                      endIndent: 0,
                                      color: Color(0xffEFEFEF),
                                    ),
                                    // 活动详情
                                    Visibility(
                                      visible: e['isviseble'],
                                      child: Container(
                                        height: setWidth(160),
                                        padding: EdgeInsets.only(
                                          left: setWidth(20),
                                          top: setWidth(14),
                                          right: setWidth(20),
                                          bottom: setWidth(40),
                                        ),
                                        color: Color(0xffFBFBFB),
                                        child: Row(
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          children: [
                                            Text(
                                              '奖励',
                                              style: TextStyle(
                                                color: Color(0xff565656),
                                                fontSize: 12,
                                              ),
                                            ),
                                            SizedBox(
                                              width: setWidth(20),
                                            ),
                                            Expanded(
                                              child: Container(
                                                height: setWidth(50),
                                                child: Column(
                                                  crossAxisAlignment: CrossAxisAlignment.start,
                                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                                  children: [
                                                    Text(
                                                      '积分：+${e['award_integral']}',
                                                      maxLines: 1,
                                                      overflow: TextOverflow.ellipsis,
                                                      style: TextStyle(
                                                        color: Color(0xff3A3A3A),
                                                        fontSize: 12,
                                                      ),
                                                    ),
                                                  ],
                                                ),
                                              ),
                                            ),
                                          ],
                                        ),
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            );
                          }).toList(),
                        )
                      ],
                    ),
                  );
                } else {
                  return Container();
                }
              },
              separatorBuilder: (context, index) {
                return Container();
              },
              itemCount: 2,
            ),
          );
        },
      ),
    );
  }
}
